/**
 * FireFly (2011)
 */
package com.firefly.kernel.plugins.config.property;

import java.util.Properties;
import java.util.StringTokenizer;
import com.firefly.kernel.exception.KernelException;
import com.firefly.kernel.plugins.bootstrap.basic.BasicKernelInitializer;
import com.firefly.kernel.plugins.config.AbstractKernelConfig;
import com.firefly.kernel.spi.bootstrap.IKernelInitializer;

/**
 * 属性文件核心配置
 * 
 * @version 1.0 2011-1-2
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since FireFly 1.0
 */
public class PropertyKernelConfig extends AbstractKernelConfig {
	/**
	 * 构造器
	 * 
	 * @param props
	 *            属性文件
	 */
	public PropertyKernelConfig(Properties props) {
	}

	@Override
	public IKernelInitializer createKernelInitializer() throws KernelException {
		return new BasicKernelInitializer();
	}

	protected Object getImplementation(String type, String defaultType) throws KernelException {
		Properties properties = getProperties();

		String value = properties.getProperty(type, defaultType);
		if (log.isTraceEnabled())
			log.trace(type + " using property " + value);

		StringTokenizer tokenizer = new StringTokenizer(value, ":");
		while (tokenizer.hasMoreTokens()) {
			String className = tokenizer.nextToken();

			ClassLoader cl = Configurator.getClassLoader((BeanMetaData) null);
			try {
				BeanInfo info = getBeanInfo(className, cl);
				BeanMetaData metaData = getBeanMetaData(info, className);
				return Configurator.instantiateAndConfigure(this, info, metaData);
			} catch (ClassNotFoundException e) {
				log.trace("不能找到类:%s,错误:%s", className, e.getMessage());
				throw new KernelException(String.format("不能找到类:%s,错误:%s", className, e.getMessage()));
			}
		}
	}
}
